package com.cet.designpatterns.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: algorithm
 * @description: 具体中介者类
 * @author: 陈恩涛
 * @create: 2021-09-23 10:35
 **/
public class ConcreteMediator extends Mediator {

    /**
     * 同事对象集合
     */
    private Map<String, Colleague> colleagueMap;
    private Map<String, Object> interMap;

    public ConcreteMediator() {
        this.colleagueMap = new HashMap<>();
        this.interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);

        if (colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (colleague instanceof TV) {
            interMap.put("TV", colleagueName);
        } else if (colleague instanceof Curtain) {
            interMap.put("Curtain", colleagueName);
        }
    }

    @Override
    public void getMessage(int stateChange, String name) {
        if (colleagueMap.get(name) instanceof Alarm) {
            if (stateChange == 0) {
                ((CoffeeMachine)colleagueMap.get(interMap.get("CoffeeMachine"))).startCoffee();
                ((TV)colleagueMap.get(interMap.get("TV"))).startTv();
            } else if (stateChange == 1) {
                ((TV)colleagueMap.get(interMap.get("TV"))).stopTv();
            }
        } else if (colleagueMap.get(name) instanceof CoffeeMachine) {
            ((Curtain)colleagueMap.get(interMap.get("Curtain"))).upCurtains();
        } else if (colleagueMap.get(name) instanceof TV) {
            // TV发出消息进行处理
        } else if (colleagueMap.get(name) instanceof Curtain) {
            //Curtain发出消息进行处理
        }

    }

    @Override
    public void sendMessage() {

    }
}
